package sharewithus.business.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import sharewithus.business.service.UserService;
import framelib.controller.AbsController;

/**
 * Title:UserController Description: test
 * 
 * @Create_by:Vince Yu
 * @Create_date:2012-5-4
 * @Last_Edit_By:
 * @Edit_Description
 * @version:partynano 1.0
 * 
 */
@Controller
// @SessionAttributes()
public class UserController extends AbsController {
	@SuppressWarnings("unchecked")
	@Autowired
	@Qualifier("userService")
	UserService userService;

	/**
	 * 
	 * @Create_by:Vince Yu
	 * @Create_date:2012-5-4
	 * @Last_Edit_By:
	 * @Edit_Description
	 * @Create_Version:partynano 1.0
	 */
	@RequestMapping(value = "/initUserLogin.htm", method = RequestMethod.GET)
	public ModelAndView initUserLoginG(ModelMap map, HttpServletRequest request) {
		String error = request.getParameter("error");
		if (error == null || error.equalsIgnoreCase("")
		        || error.equalsIgnoreCase("null")) {
			try {
				userService.saveRefererUrlNN(request);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			map.put("error", error);
		}

		return new ModelAndView("userLogin", map);
	}

	/**
	 * Title:initUserLoginP Description:用户登录失败跳转
	 * 
	 * @Create_by:Vince Yu
	 * @Create_date:2012-5-7
	 * @Last_Edit_By:
	 * @Edit_Description
	 * @Create_Version:ShareWithUs 1.0
	 */
	@RequestMapping(value = "/initUserLogin.htm", method = RequestMethod.POST)
	public ModelAndView initUserLoginP(ModelMap map, HttpServletRequest request) {
		return new ModelAndView("userLogin", map);
	}

	/**
	 * Title:userLogin Description:用户登录
	 * 
	 * @Create_by:Vince Yu
	 * @Create_date:2012-5-7
	 * @Last_Edit_By:
	 * @Edit_Description
	 * @Create_Version:ShareWithUs 1.0
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/userLogin.htm")
	public void userLogin(@RequestParam("userName") String userName,
	        @RequestParam("password") String password, ModelMap map,
	        HttpServletRequest request, HttpServletResponse response) {
		try {
			userService.checkPassword(userName, password, request);
			response.getWriter().write("success");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				response.getWriter().write(e.getMessage());
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	/**
	 * Title:userLogout Description:用户注销操作
	 * 
	 * @Create_by:Vince Yu
	 * @Create_date:2012-5-11
	 * @Last_Edit_By:
	 * @Edit_Description
	 * @Create_Version:ShareWithUs 1.0
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/userLogout.htm")
	public ModelAndView userLogout(ModelMap map, HttpServletRequest request) {
		try {
			userService.userLogout(request);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("redirect:initIndex.htm");
	}

	/**
	 * Title:register Description:用户注册
	 * 
	 * @Create_by:Vince Yu
	 * @Create_date:2012-5-7
	 * @Last_Edit_By:
	 * @Edit_Description
	 * @Create_Version:ShareWithUs 1.0
	 */
	@RequestMapping(value = "/register.htm")
	public ModelAndView register(@RequestParam("email") String email,
	        @RequestParam("nickname") String nickname,
	        @RequestParam("password") String password,
	        @RequestParam("sex") String sex,
	        @RequestParam("province") String province,
	        @RequestParam("city") String city,
	        @RequestParam("county") String county,
	        @RequestParam("YYYY") String birthYear,
	        @RequestParam("MM") String birthMonth,
	        @RequestParam("DD") String birthDay, ModelMap map,
	        HttpServletRequest request) {

		try {
			userService.registeUser(email, nickname, password, sex, province,
			        city, county, birthYear, birthMonth, birthDay);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ModelAndView("userLogin", map);
	}

	/**
	 * Title:initRegister Description:初始化用户注册页
	 * 
	 * @Create_by:Vince Yu
	 * @Create_date:2012-5-8
	 * @Last_Edit_By:
	 * @Edit_Description
	 * @Create_Version:ShareWithUs 1.0
	 */
	@RequestMapping(value = "/initRegister.htm")
	public ModelAndView initRegister(ModelMap map, HttpServletRequest request) {
		return new ModelAndView("userRegister", map);
	}

	/**
	 * Title:checkUser Description:检查昵称是否可用
	 * 
	 * @Create_by:Vince Yu
	 * @Create_date:2012-5-9
	 * @Last_Edit_By:
	 * @Edit_Description
	 * @Create_Version:ShareWithUs 1.0
	 */
	@RequestMapping(value = "/checkUser.htm")
	public void checkUser(@RequestParam("nickname") String nickname,
	        ModelMap map, HttpServletRequest request,
	        HttpServletResponse response) {
		try {
			if (userService.checkUserNN(nickname, request)) {
				response.getWriter().write("true");
			} else {
				response.getWriter().write("fail");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
		}
	}

	/**
	 * Title:checkEmail Description:检查email是否可用
	 * 
	 * @Create_by:Vince Yu
	 * @Create_date:2012-5-9
	 * @Last_Edit_By:
	 * @Edit_Description
	 * @Create_Version:ShareWithUs 1.0
	 */
	@RequestMapping(value = "/checkEmail.htm")
	public void checkEmail(@RequestParam("email") String email, ModelMap map,
	        HttpServletRequest request, HttpServletResponse response) {
		try {
			if (userService.checkEmailNN(email, request)) {
				response.getWriter().write("true");
			} else {
				response.getWriter().write("fail");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
		}
	}

	/***************************** userInfo ************************************/
	/**
	 * Title:initUserInfo Description:初始化用户信息页
	 * 
	 * @Create_by:Vince Yu
	 * @Create_date:2012-5-12
	 * @Last_Edit_By:
	 * @Edit_Description
	 * @Create_Version:ShareWithUs 1.0
	 */
	@RequestMapping(value = "/user/initUserInfo.htm")
	public ModelAndView initUserInfo(ModelMap map, HttpServletRequest request,
	        HttpServletResponse response) {

		return new ModelAndView("userInfo");
	}

	/**
	 * Title:updateUserInfo Description:更新用户基本信息
	 * 
	 * @Create_by:Vince Yu
	 * @Create_date:2012-5-13
	 * @Last_Edit_By:
	 * @Edit_Description
	 * @Create_Version:ShareWithUs 1.0
	 */
	@RequestMapping(value = "/user/updateUserInfo.htm")
	public void updateUserInfo(@RequestParam("userId") String userId,
	        @RequestParam("email") String email,
	        @RequestParam("nickname") String nickname,
	        @RequestParam("sex") String sex,
	        @RequestParam("province") String province,
	        @RequestParam("city") String city,
	        @RequestParam("county") String county,
	        @RequestParam("YYYY") String birthYear,
	        @RequestParam("MM") String birthMonth,
	        @RequestParam("DD") String birthDay, ModelMap map,
	        HttpServletRequest request, HttpServletResponse response) {

		try {
			userService.updateUserInfo(userId, email, nickname, sex, province,
			        city, county, birthYear, birthMonth, birthDay, request);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * Title:initUserAvatar Description:初始化用户头像修改页
	 * 
	 * @Create_by:Vince Yu
	 * @Create_date:2012-5-13
	 * @Last_Edit_By:
	 * @Edit_Description
	 * @Create_Version:ShareWithUs 1.0
	 */
	@RequestMapping(value = "/user/initUserAvatar.htm")
	public ModelAndView initUserAvatar(ModelMap map,
	        HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("userAvatar");
	}

	/**
	 * Title:initUserAvatar Description:初始化用户头像修改页
	 * 
	 * @Create_by:Vince Yu
	 * @Create_date:2012-5-13
	 * @Last_Edit_By:
	 * @Edit_Description
	 * @Create_Version:ShareWithUs 1.0
	 */
	@RequestMapping(value="/user/userAvatarUpload.htm")
    public void userAvatarUpload(
    		ModelMap map,HttpServletRequest request,HttpServletResponse response) {
		Map res = new HashMap();
        try {
	        res = userService.uploadUserAvatar(request);
	        response.getWriter().write(
					"path="+String.valueOf(res.get("fileName"))
					+","
					+"error=" +String.valueOf(res.get("error"))
					+","
					+"width=" +String.valueOf(res.get("width"))
					+","
					+"height=" +String.valueOf(res.get("height"))
	        );
        } catch (Exception e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
        }
	}
	
	
	
	/**
	 * Title:saveUserAvatar
	 * Description:保存用户头像
	 * @Create_by:Vince Yu
	 * @Create_date:2012-5-14
	 * @Last_Edit_By:
	 * @Edit_Description
	 * @Create_Version:ShareWithUs 1.0
	 */
	@RequestMapping(value="/user/saveUserAvatar.htm")
    public void saveUserAvatar(
    		@RequestParam("filePath") String filePath,
    		@RequestParam("x1") int x1,
    		@RequestParam("y1") int y1,
    		@RequestParam("x2") int x2,
    		@RequestParam("y2") int y2,
    		@RequestParam("w") int w,
    		@RequestParam("h") int h,
    		ModelMap map,HttpServletRequest request,HttpServletResponse response){
		try {
	        userService.saveUserAvatar(filePath, x1, y1, x2, y2, w, h, request);
	        response.getWriter().write("success");
        } catch (Exception e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
        }
	}
}